package activity.store;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.itboye.hutoubenjg.R;

import util.BaseOtherActivity;
import util.utls.AutoResizeTextView;

/**
 * 银联支付
 */
public class YinLianWebActivity extends BaseOtherActivity {
    ImageView back;// 返回图片
    AutoResizeTextView tvTitle;// 标题
    ProgressBar myProgressBar;
    WebView webview;
    String url="";
    String title="";

    @Override
    protected int layout_id() {
        return R.layout.activity_web;
    }

    @Override
    protected void onInitController() {

    }


    @Override
    protected void onInit() {
        url=getIntent().getStringExtra("url");
        title=getIntent().getStringExtra("title");
        tvTitle.setText(title);

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });


        //支持javascript
        webview.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webview.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webview.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放(设置WebView支持viewpoart)
        webview.getSettings().setUseWideViewPort(true);
        //自适应屏幕(设置网页超过屏幕宽度时重新布局为屏幕宽度)
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.loadUrl(url);


        //如果不设置WebViewClient，请求会跳转系统浏览器
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //该方法在Build.VERSION_CODES.LOLLIPOP以前有效，从Build.VERSION_CODES.LOLLIPOP起，建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242

                if (url.toString().contains("pay_code")){
                    view.loadUrl(url);
                    return true;
                }
                if (url.contains("BACK")) {
                    Intent intent1 = new Intent("zhifu.jieshu");
                    sendBroadcast(intent1);
                    finish();
                }  else  {
                    view.loadUrl(url);
                }

                return false;
            }

        });
        webview.loadUrl(url);
    }
    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }
}
